package ${classPackage!};

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

<#if classImports??>
<#list classImports as importItem>
import ${importItem};
</#list>
</#if>
import com.listeningframework.boot.autoconfigure.orm.mybatis.support.ListeningMybatisServiceAdapter;
import com.listeningframework.boot.domains.Pagination;
import com.listeningframework.boot.domains.Paging;
import com.listeningframework.boot.exception.BusinessException;

/**
 * 
 * @description ${modelDesc!} Service
 *
 * @author Listening
 * @version $Id: ${modelPrefix!}Service.java, v 0.1 ${currentDateTime!} Listening Exp $
 */
@Component
public class ${modelPrefix!}Service extends ListeningMybatisServiceAdapter<${modelPrefix!}${modelSuffix!}, Long> {
    /**
     * ${modelPrefix} Reposotory
     */
    @Autowired
    private ${modelPrefix!}Repository ${modelPrefix?uncap_first}Repository;

    /**
     * 
     * @see com.listeningframework.boot.autoconfigure.orm.mybatis.support.ListeningMybatisServiceAdapter#page(java.lang.Object, com.listeningframework.boot.domains.Paging)
     */
    public Pagination<${modelPrefix!}${modelSuffix!}> page(${modelPrefix!}${modelSuffix!} domain, Paging page) throws BusinessException {
        if (page == null || page.hasNoParam()) {
            throw new BusinessException("分页查询页码不能为空");
        }
        try {
            return ${modelPrefix?uncap_first}Repository.page(domain, page);
        } catch (Exception e) {
            throw new BusinessException("分页查询失败", e);
        }
    }

    /**
     * 
     * @see com.listeningframework.boot.autoconfigure.orm.mybatis.support.ListeningMybatisServiceAdapter#save(java.lang.Object)
     */
    @Override
    public Integer save(${modelPrefix!}${modelSuffix!} domain) throws BusinessException {
        if (domain == null) {
            throw new BusinessException("保存对象不能为空");
        }
        if (domain.getId() == null) {
            // ID为空执行插入
            Integer count = null;
            try {
                count = ${modelPrefix?uncap_first}Repository.insert(domain);
            } catch (Exception e) {
                throw new BusinessException("插入数据异常, 请稍后再试", e);
            }
            if (count == null || count <= 0) {
                throw new BusinessException("成功插入0条记录, 请联系管理员");
            }
            return count;
        } else {
            // ID非空执行更新操作
            Integer count = null;
            try {
                count = ${modelPrefix?uncap_first}Repository.update(domain);
            } catch (Exception e) {
                throw new BusinessException("修改数据异常, 请稍后再试", e);
            }
            if (count == null || count <= 0) {
                throw new BusinessException("修改数据失败");
            }
            return count;
        }
    }

    /**
     * 
     * @see com.listeningframework.boot.autoconfigure.orm.mybatis.support.ListeningMybatisServiceAdapter#delete(java.io.Serializable)
     */
    @Override
    public Integer delete(Long id) throws BusinessException {
        if (id == null) {
            throw new BusinessException("删除操作ID不能为空");
        }
        Integer count = null;
        try {
            count = ${modelPrefix?uncap_first}Repository.delete(id);
        } catch (Exception e) {
            throw new BusinessException("删除操作失败, 请稍后再试", e);
        }
        if (count == null || count <= 0) {
            throw new BusinessException("成功删除0条记录, 请联系管理员");
        }
        return count;
    }

    /**
     * 
     * @see com.listeningframework.boot.autoconfigure.orm.mybatis.support.ListeningMybatisServiceAdapter#select(java.io.Serializable)
     */
    @Override
    public ${modelPrefix!}${modelSuffix!} select(Long id) throws BusinessException {
        try {
            return ${modelPrefix?uncap_first}Repository.select(id);
        } catch (Exception e) {
            throw new BusinessException("主键查询数据失败", e);
        }
    }

    /**
     * 
     * @see com.listeningframework.boot.autoconfigure.orm.mybatis.support.ListeningMybatisServiceAdapter#filter(java.lang.Object)
     */
    public List<${modelPrefix!}${modelSuffix!}> filter(${modelPrefix!}${modelSuffix!} domain) throws BusinessException {
        try {
            return ${modelPrefix?uncap_first}Repository.filter(domain);
        } catch (Exception e) {
            throw new BusinessException("条件查询列表失败", e);
        }
    }

}
